package com.example.controller;

import com.example.service.BorderSize;

import javax.swing.*;
import java.awt.*;

public class ChessBorder extends JPanel implements BorderSize {
    public void showBorder (){
        JFrame frame = new JFrame();
        frame.setTitle("五子棋");
        frame.setSize(1100,1100);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ImageIcon centerIcon = new ImageIcon("./src/img/timg (1).jpg");
        centerIcon.setImage(centerIcon.getImage().getScaledInstance(1100,1100,Image.SCALE_AREA_AVERAGING));
        JLabel centerLabel = new JLabel();
        centerLabel.setIcon(centerIcon);
        centerLabel.setSize(1100,1100);
        this.add(centerLabel);
        //设置容器为边框布局
        frame.setLayout(new BorderLayout());
        //this.setBackground(Color.LIGHT_GRAY);
        frame.add(this, BorderLayout.CENTER);
        //添加按钮面板并设置大小
        JPanel eastPanel = new JPanel();
        eastPanel.setBackground(Color.DARK_GRAY);
        Dimension eastPsize = new Dimension(120,1000);
        eastPanel.setPreferredSize(eastPsize);
        frame.add(eastPanel, BorderLayout.EAST);
        //添加标签，使按钮居中
        JLabel label = new JLabel();
        Dimension labelSize = new Dimension(110,300);
        label.setPreferredSize(labelSize);
        eastPanel.add(label);
        //创建监听器对象
        BorderListener BL = new BorderListener();
        //添加鼠标监听器到棋盘面板
        this.addMouseListener(BL);
        //设置按钮的大小
        Dimension buttonSize = new Dimension(110,50);
        String[] btn ={"开始新游戏","认输","悔棋"};
        for(int i=0;i<btn.length;i++){
            JButton button = new JButton(btn[i]);
            button.setPreferredSize(buttonSize);
            button.addActionListener(BL);
            eastPanel.add(button);
        }
        String[] item = {"人人对战","人机对战"};
        JComboBox<String> JCB = new JComboBox<String>(item);
        JCB.setPreferredSize(buttonSize);
        eastPanel.add(JCB);
        JCB.addActionListener(BL);

        //显示可见
        frame.setVisible(true);
        BL.setPanel(this);
        BL.setJCB(JCB);
        BL.setCHB(this);
    }

    public void paint(Graphics g) {
        super.paint(g);
        for (int i = 0; i < ROW; i++) {
            //画横线
            g.drawLine(X, Y + SIZE * i, X + (ROW - 1) * SIZE, Y + SIZE * i);
            g.drawLine(X + SIZE * i, Y, X + SIZE * i, Y + SIZE * (COLUM - 1));
        }
        //画棋子
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COLUM; j++) {
                if (chessArray[i][j] == 1) {
                    g.setColor(Color.BLACK);
                    g.fillOval(X + i * SIZE - SIZE / 2, Y + j * SIZE - SIZE / 2, SIZE, SIZE);
                }
                if (chessArray[i][j] == 2) {
                    g.setColor(Color.WHITE);
                    g.fillOval(X + i * SIZE - SIZE / 2, Y + j * SIZE - SIZE / 2, SIZE, SIZE);
                }
            }
        }
    }

    public static void main(String[] args){
        ChessBorder CHB = new ChessBorder();
        CHB.showBorder();
    }
}

